<?php
//信息分类控制器
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Org\Upload;


class NewbookController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $db = \DB::table("newbook");
		$list = $db->paginate(3); //5条每页浏览
        
        //处理信息
        return view("admin.newbook.index",['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
		$list = \DB::select("select * from classone order by concat(pidstr,id) asc");
		
		//处理信息
        foreach($list as &$v){
            $m = substr_count($v->pidstr,","); //获取path中的逗号
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
        }
		return view("admin.newbook.create",['list'=>$list]);
		
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
		if(empty($_POST['name']||$_POST['auth']||$_POST['newprice']||$_POST['oldprice']||$_POST['type']||$_POST['pid'])){
			$info = "请填写完整商品信息！";
			return view("admin.newbook.info",['info'=>$info]);
		}
		
		//实例化上传对象
		if($_FILES['myfile']['error']==4){
			$info = "请上传书籍图片！";
			return view("admin.newbook.info",['info'=>$info]);
		}else{
			//事务回滚
			\DB::transaction(function(){
				$upfile = new Upload("myfile");
				
				//获取添加信息
				$name = $_POST['name'];
				$auth = $_POST['auth'];
				$newprice = $_POST['newprice'];
				$oldprice = $_POST['oldprice'];
				$type = $_POST['type'];
				$pid = $_POST['pid'];
				//初始化上传信息
				$upfile->path="./img/home/"; //上传储存路径
				$upfile->typeList =array("image/jpeg","image/png","image/gif"); //设置允许上传类型
				$upfile->maxSize =0; //允许上传大小
		
				//执行文件上传
				$res = $upfile->doUpload();
		
				//判断输出
				if($res){
				
					//执行数据库添加
					$src = $upfile->saveName; 
						
					$id = \DB::table('newbook')->insertGetId(['name'=>$name,'src'=>$src,'auth'=>$auth,'newprice'=>$newprice,'oldprice'=>$oldprice,'type'=>$type,'pid'=>$pid]);
					$sid = \DB::table('goodsdetail')->insertGetId(['goodName'=>$name,'pic1'=>$src,'auth'=>$auth,'youhuiPrice'=>$newprice,'marketPrice'=>$oldprice,'pid'=>$id]);
					if($sid<1){
						$info = "上传失败！";
						return view("admin.newbook.info",['info'=>$info]);
						\DB::rollback();//事务回滚
					}
				}
			});
			$db = \DB::table("newbook");
			$list = $db->paginate(3); //5条每页浏览
        
			//处理信息
			return view("admin.newbook.index",['list'=>$list]);
		}
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $list = \DB::table('newbook')->where('id',$id)->first();
		return view("admin.newbook.edit",['list'=>$list]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //实例化上传对象
		if($_FILES['myfile']['error']==4){
			$name = $_POST['name'];
			$auth = $_POST['auth'];
			$newprice = $_POST['newprice'];
			$oldprice = $_POST['oldprice'];
			//$add = $_POST['add'];
			//$title = $_POST['title'];
			$type = $_POST['type'];
			$affected = \DB::table('newbook')->where('id', $id)->update(['name'=>$name,'auth'=>$auth,'newprice'=>$newprice,'oldprice'=>$oldprice,'type'=>$type]);
			if($affected<1){
				$info = "修改失败！";
				return view("admin.newbook.info",['info'=>$info]);
			}
			return redirect("admin/newbook");
		}else{
			$upfile = new Upload("myfile");
	
			//初始化上传信息
			$upfile->path="./img/home/"; //上传储存路径
			$upfile->typeList =array("image/jpeg","image/png","image/gif"); //设置允许上传类型
			$upfile->maxSize =0; //允许上传大小
	
			//执行文件上传
			$res = $upfile->doUpload();
	
			//判断输出
			if($res){
				
				//执行数据库添加
				$src = $upfile->saveName;
				$name = $_POST['name'];
				$auth = $_POST['auth'];
				$newprice = $_POST['newprice'];
				$oldprice = $_POST['oldprice'];
				//$add = $_POST['add'];
				$title = $_POST['title'];
				$type = $_POST['type'];
				$affected = \DB::table('newbook')->where('id', $id)->update(['name'=>$name,'src'=>$src,'auth'=>$auth,'newprice'=>$newprice,'oldprice'=>$oldprice,'title'=>$title]);
				if($affected<1){
					$info = "修改失败！";
					return view("admin.newbook.info",['info'=>$info]);
				}
					return redirect("admin/newbook");
			}else{
				$info = "上传失败！";
				return view("admin.newbook.info",['info'=>$info]);
				}
		}
	}

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
		\DB::table('newbook')->delete($id);
		return redirect("admin/newbook");
    }
	
	public function fenlei()
	{
		//获取点击的id值
        $me = $_GET['id'];

        //处理成数据库中的数字前缀
        $me = $me+0;

        //定义空字符串
        $data = '';

        //得到数据库里的东西
        $list = \DB::table('classone')->where('pid',$me)->get();
		foreach($list as $v){

            // $data = $v->name = "<li><a href=javascript:; id='".$v->id."city'>".$v->name.'</a></li>'.$data;
            $data = "<li><a href=javascript:; id='".$v->id."city'>".$v->name.'</a></li>'.$data;
        }
        return $data;
	}
}
